//
//  MCgoodsListShowSearchViewController.swift
//  StoreManage
//
//  Created by MC on 2018/9/4.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit


class MCgoodsListShowSearchViewController: MCSearchBaseViewController {
    
    private var pageNumber = 1
    private var searchWord = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc override func rightItemClicked() {
        searchTextField.resignFirstResponder()
        searchWord = searchTextField.text ?? ""
        pageNumber = 1
        sendNetworking()
    }
    
    // MARK: - Network
    func sendNetworking() {
        
        
        let para = [
            "pageNum"    : pageNumber,
            "keyWord"    : searchWord,
            "type"           : 1,
            "pageSize"   : 20
            ] as [String : Any]

        //        MCHUD_default(message: "加载中...", view: view, delay: 0)
        
        MCNetwork.POST(JAVA_queryGoodsBySelfCategoryId, para, vc: self, success: { (data) in
            
            //            MCHUD_hidden(view: self.view)

            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            if self.pageNumber == 1 {
                self.dataArrayM.removeAllObjects()
            }
            
            let dataArray = data.arrayObject ?? []
            
            
            if dataArray.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                for dict in dataArray {
                    let model = MCGoodsListModel()
                    model.setValuesForKeys(dict as! [String : Any])
                    self.dataArrayM.add(model)
                }
            }
            self.tableView.reloadData()

            
        }, failure: nil)

        
        
        
        
        

    }
    
    // MARK: - Method
    func baseSetting() {
        self.view.backgroundColor = UIColor.MCGray_light
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNumber = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNumber += 1
            self.sendNetworking()
        })
        
        
        searchTextField.closure = { () -> Void in
            self.rightItemClicked()
        }
    }
    
    func initUI() {
        
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
    }
    // MARK: - Setter & Getter
    private lazy var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.backgroundColor = UIColor.white
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray.init()
        return arrayM
    }()
}


extension MCgoodsListShowSearchViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 136
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCgoodsListShowCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.row = indexPath.row
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.model = self.dataArrayM[indexPath.row] as! MCGoodsListModel
        
        cell.closure = { row in
            let model = self.dataArrayM[row] as! MCGoodsListModel
            
            let vc = MCShareGoodsViewController()
            vc.goodsId = model.goodsId
            vc.goodsName = model.goodsName
            vc.lowestPrice = model.lowestPrice
            vc.suggestedPrice = model.suggestedPrice
            vc.gooodsImage = model.navigateImage
            
            MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
            
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.dataArrayM[indexPath.row] as! MCGoodsListModel
        let vc = MCGoodsDetailViewController()
        vc.isOnShelves = 1000
        vc.goodsId = model.goodsId
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
}

